package Compras;

import Productos.ProductosBD;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

public class ComprasBD {

    
    
     java.sql.Connection  conexion = null;
     
     public ComprasBD(){
          try {
            conexion = DriverManager.getConnection("jdbc:mysql://localhost:3306/Certcosa","root","root");
        } catch (SQLException ex) {
         JOptionPane.showMessageDialog(null,"no conecta!! Error!!! o.O!");   
        }
     }
     
     public int GetIdCompra() throws SQLException{
         java.sql.ResultSet rs = null ;
          try {
              Statement cs = conexion.createStatement();
              rs = cs.executeQuery("SELECT  MAX(idFacturaCompra) as IdFV FROM Certcosa.FacturaCompra ;");
         }catch (SQLException ex) {
              Logger.getLogger(ProductosBD.class.getName()).log(Level.SEVERE, null, ex);
        }
         return rs.getInt("IdFV");     
         
     }
     
     public void InsertarFCompra(Date fecha,String total, String nitProv){
          try 
         {
             Statement cs = conexion.createStatement();
             
             cs.execute("INSERT INTO Certcosa.FacturaVenta VALUES (" + fecha+ ","+ total+","+ nitProv+");");
                        
             
           //  JOptionPane.showMessageDialog(null,"Se ha ingresado el Producto exitosamente!");
        } catch (SQLException ex) {
            //JOptionPane.showMessageDialog(null,"Ha ocurrido un error con la inserción.!!");
        }
     }
     
     public void InsertarDetalleCompra(String idProd,  String precio){
        try {
            int idFact = GetIdCompra();
            Statement cs = conexion.createStatement();
            cs.execute("INSERT INTO Certcosa.DetalleFacturaCompra VALUES ("+precio + ","+ idFact + ","+ idProd + ");");
        } catch (SQLException ex) {
            Logger.getLogger(ComprasBD.class.getName()).log(Level.SEVERE, null, ex);
        }        
         
     }
     
     public void InsertarLote(String prod, String cant, String fecha, String precio){
          try {
            int idFact = GetIdCompra();
            Statement cs = conexion.createStatement();
            cs.execute("INSERT INTO Certcosa.Lote VALUES ("+precio + ","+cant+","+cant+","+fecha + ","+ prod + ");");
        } catch (SQLException ex) {
            Logger.getLogger(ComprasBD.class.getName()).log(Level.SEVERE, null, ex);
        }    
         
         
         
     }
    
}
